package com.mybatis.generator.codegen;

import com.google.common.collect.Maps;
import com.mybatis.generator.bean.GenColumn;
import com.mybatis.generator.bean.GenTable;
import com.mybatis.generator.config.CodeGeneratorConfig;
import com.mybatis.generator.db.GenDb;
import com.mybatis.generator.utils.GenUtil;
import freemarker.template.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class CodeGenerator {

    private static Logger logger = LoggerFactory.getLogger(CodeGenerator.class);

    private CodeGeneratorConfig config;
    private GenDb db;

    // 已存在是否需要覆盖
    private static boolean isOverwrite = true;
    private Configuration cfg;
    private Map<String, Object> variables = Maps.newHashMap();
    private String className;

    public CodeGenerator(CodeGeneratorConfig config) {
        try {
            this.config = config;
            cfg = new Configuration(Configuration.VERSION_2_3_28);
            cfg.setDirectoryForTemplateLoading(new File(config.getTemplatePath()));
            cfg.setDefaultEncoding("UTF-8");

            variables.put("author", config.getCodeCommentsAuthor());
            variables.put("date", config.getCreateDate());

            db = new GenDb(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:根据数据库表批量代码生产
     */
    public void genAllTable() throws Exception {
        List<GenTable> talbes = db.getAllTableName();
        for (GenTable tableName : talbes) {
            genSingleTable(tableName);
        }

//        List<GenTable> talbes = new ArrayList<>();
//        GenTable genTable1 = new GenTable();
//        genTable1.setTableName("org_app_cfg");
//        genTable1.setTableComment("组织自建crm应用配置表");
//
//        GenTable genTable2 = new GenTable();
//        genTable2.setTableName("org_temail_app_cfg");
//        genTable2.setTableComment("组织与秘邮应用号配置表");
//
//        talbes.add(genTable1);
//        talbes.add(genTable2);
//
//        for (GenTable tableName : talbes) {
//            genSingleTable(tableName);
//        }
    }

    /**
     * 功能描述: 制定数据库表单个代码生产
     */
    public void genSingleTable(GenTable genTable) throws Exception {
        List<GenColumn> columns = db.getTableNameColumn(genTable.getTableName());
        if(StringUtils.isBlank(config.getTableNameReplaceChar())){
            className = GenUtil.replaceUnderlineAndfirstToUpper(genTable.getTableName(), config.getTableNameSeparator(), "");
        } else {
            className = GenUtil.replaceUnderlineAndfirstToUpper(genTable.getTableName().replace(config.getTableNameReplaceChar(), ""), config.getTableNameSeparator(), "");
        }
        className = GenUtil.firstCharacterToLower(className);
        variables.put("className", className);
        variables.put("columns", columns);
        variables.put("tableName", genTable.getTableName());
        variables.put("keyColumn", columns.get(0));
        variables.put("package", config.getClassBasePackage());
        variables.put("tableComment", genTable.getTableComment());

        CodeGeneratorDao generatorDao = new CodeGeneratorDao(cfg, config, className);
        generatorDao.build(variables, isOverwrite);

        CodeGeneratorService generatorService = new CodeGeneratorService(cfg, config, className);
        generatorService.build(variables, isOverwrite);

        CodeGeneratorController generatorController = new CodeGeneratorController(cfg, config, className);
        generatorController.build(variables, isOverwrite);
    }

}
